package com.smartkey.ui;

import java.io.File;
import java.io.FileOutputStream;

import com.project.smartkey.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

public class ScreentShotActivity extends Activity {

	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		doScreentShot();
	}

	private void initView() {
		setContentView(R.layout.screenshot_activity);
		imageView = (ImageView) findViewById(R.id.img_screentshot);
	}

	private void doScreentShot() {
		Bitmap bitmap = screentShot_01(); //screenShot();
		if (null != bitmap) {
			imageView.setImageBitmap(bitmap);
		}
		// finish();
	}

	private Bitmap screentShot_01() {
		try {
			String savePath = getSDCardPath() + "/screentshot/";
			// 保存Bitmap   
			// 文件  
			String filePath = savePath + System.currentTimeMillis() + ".png";
			View decorview = getWindow().getDecorView().getRootView();
			Bitmap bitmap = Bitmap.createBitmap(decorview.getWidth(),
					decorview.getHeight(), Config.ARGB_8888);
			Canvas canvas = new Canvas();
			canvas.setBitmap(bitmap);
			decorview.draw(canvas);

			try {
				FileOutputStream fos = new FileOutputStream(filePath);
				bitmap.compress(CompressFormat.PNG, 100, fos);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return bitmap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private Bitmap screenShot() {
		// 构建Bitmap  
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		int w = display.getWidth();
		int h = display.getHeight();
		Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
		// 获取屏幕
		View decorview = getWindow().getDecorView().getRootView();
		decorview.setDrawingCacheEnabled(true);
		decorview.buildDrawingCache();
		bitmap = decorview.getDrawingCache();
		// 图片存储路径
		String savePath = getSDCardPath() + "/screentshot/";
		// 保存Bitmap   
		try {
			// 文件  
			String filePath = savePath + System.currentTimeMillis() + ".png";
			File file = new File(filePath);
			if (!file.exists()) {
				file.mkdirs();
				file.createNewFile();
			}

			FileOutputStream fileOutputStream = new FileOutputStream(file);
			if (null != fileOutputStream) {
				bitmap.compress(Bitmap.CompressFormat.PNG, 100,
						fileOutputStream);
				fileOutputStream.flush();
				fileOutputStream.close();
			}
			Toast.makeText(this, "截屏文件已保存至SDCard/ScreenImages/目录下",
					Toast.LENGTH_LONG).show();
		} catch (Exception e) {
		}
		return bitmap;
	}

	/**
	 * 获取SDCard的目录路径功能
	 * 
	 * @return
	 */
	private String getSDCardPath() {
		File sdcardDir = null;
		// 判断SDCard是否存在
		boolean sdcardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		if (sdcardExist) {
			sdcardDir = Environment.getExternalStorageDirectory();
		}
		return sdcardDir.toString();
	}
}
